#include <cctype>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;

//map写配置文件

class AliyunConfig{
public:
    static AliyunConfig * getInstance(){
        pthread_once(&_once,init);
        return _ps;
    }

    void print(){
        cout << this << endl;
    }
private:
    static void destory(){
        if(_ps){
            delete _ps;
            _ps = nullptr;
        }
    }

public:
    void put(const string & key,const string & value){
        _conf[key] = value;
    }
    
    void get(const string & key) const{
        auto it = _conf.find(key);
        if(it != _conf.end()){
            cout << it->first << "->" << it->second << endl;
        }else{
            cerr << "not find" << endl;
        }
    }
private:
    AliyunConfig(){}
    ~AliyunConfig(){}
    AliyunConfig(const AliyunConfig & rhs) = delete;
    AliyunConfig & operator=(const AliyunConfig & rhs) = delete;
    static AliyunConfig * _ps;
    static pthread_once_t _once;
    static void init(){
        _ps = new AliyunConfig();
        atexit(destory);
    }

    map<string,string> _conf;
};
AliyunConfig * AliyunConfig::_ps = nullptr;
pthread_once_t AliyunConfig::_once = PTHREAD_ONCE_INIT;

void test(){
    ifstream ifs("aliyun.conf");
    if(!ifs){
        cerr << "file is not found" << endl;
        return;
    }

    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key,value;
        iss >> key >> value;
        AliyunConfig::getInstance()->put(key,value);
    }

    AliyunConfig::getInstance()->get("ip");
    //AliyunConfig::getInstance()->print();
    
    AliyunConfig::getInstance()->get("port");
    //AliyunConfig::getInstance()->print();

    //AliyunConfig::getInstance()->put("port","8080");
    //AliyunConfig::getInstance()->get("port");
}

int main()
{
    test();
    return 0;
}

